{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd0400673a8ac0ec28aa8cb57314b4cf61dae73602632d5d13300e749f49a50fa05",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "自定义函数"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cacul(x,y,method = 'plus'): #第三个参数若不传入，则默认为plus\n",
    "    if method == 'plus':\n",
    "        return x+y\n",
    "    elif method == 'minus':\n",
    "        return x-y\n",
    "    elif method == 'time':\n",
    "        return x*y\n",
    "    elif method == 'divide':\n",
    "        return x/y\n",
    "    else:\n",
    "        print('error method!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "cacul(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "cacul(2,3,'time')"
   ]
  },
  {
   "source": [
    "map 函数"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(i):\n",
    "    return i*i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "[func(i) for i in range(1,6)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "list(map(func,range(1,6))) #map "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['1', '2', '3', '4', '5']"
      ]
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "list(map(str,range(1,6)))"
   ]
  },
  {
   "source": [
    "lambda函数，eg:lambda x:x*x  输入x输出x*x"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[11, 12, 13, 14, 15]"
      ]
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "list(map(lambda x:x+10,range(1,6)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}